package main

import (
	"fmt"
	"strconv"
	"strings"
)

//#link: https://leetcode-cn.com/problems/valid-number/
func main() {
	s := "3."
	fmt.Println(isNumber(s))
}

func isNumber(s string) bool {
	if strings.Count(s, "-")+strings.Count(s, "+") <= 1 {
		//说明是小数
		if strings.Contains(s, ".") {
			//点在第一位
			if strings.LastIndex(s, ".") == 0 {
				return isNum(s[strings.Index(s, ".")+1:])
			}
			//点在最后一位
			if strings.Index(s, ".") == len(s)-1 {
				return isNum(s[:len(s)-1])
			}
			if index := strings.Index(s, "."); isNum(s[:index]) {
				if strings.Contains(s[index:], "e") || strings.Contains(s[index:], "E") {
					return isNum(s[index:strings.Index(s, "e")])
				} else {
					return isNum(s[index:])
				}
			}
		} else {
			return isNum(s)
		}
	}
	return false
}

func isNum(s string) bool {
	if strings.Contains(s, "+") && strings.LastIndex(s, "+") != 0 {
		return false
	}
	if strings.Contains(s, "-") && strings.LastIndex(s, "-") != 0 {
		return false
	}
	s = strings.ReplaceAll(s, "+", "")
	s = strings.ReplaceAll(s, "-", "")
	if len(s) > 0 {
		_, err := strconv.Atoi(s)
		if err != nil {
			return false
		}
		return true
	}
	return false
}
